﻿using System;
using System.Collections.Generic;
using System.Web.Http;
using AttributeRouting.Web.Http;
using WS.EKA.Core.DI;
using WS.EKA.Model;
using WS.EKA.ServiceInterface;
using WS.EKA.Utilities.Consts;
using WS.EKA.Portal.Filters;
using System.Net;

namespace WS.EKA.Portal.Controllers
{
    public class OrderController : ControllerBase
    {
        static object locker = new object();
        private IOrder orderService;
        private IDispatchMode dispatchModeService;
        private IPayment paymentService;
        public OrderController(IOrder order,IDispatchMode dispatchMode,IPayment payment)
        {
            this.orderService = order;
            this.dispatchModeService = dispatchMode;
            this.paymentService = payment;
        }
        [GET("api/getorderno")]
        public string GetOrderNumber()
        {
            lock (locker)
            {
                return DateTime.Now.ToString("yyyyMMddHHmmssffff");
            }
        }

        [GET("api/order/member/{loginId}")]
        public List<Order> GetOrderByMember(string loginId)
        {
            return orderService.GetOrderByMember(loginId);
        }
        [GET("api/order/{orderCode}")]
        public Order GetOrder(string orderCode)
        {
            return orderService.GetOrderByCode(orderCode);
        }

        public HttpStatusCode Post(Order model)
        {
            var userName = GetCurrentUser();
            model.Guid = Guid.NewGuid();
            model.CreateUser = userName;
            model.MemLoginID = userName;
            orderService.Add(model);
            return HttpStatusCode.Created;
        }

        public HttpStatusCode Put(Order model)
        {
            model.ModifyUser = GetCurrentUser();
            orderService.Update(model);
            return HttpStatusCode.Accepted;
        }

        [PUT("api/order/price/caculate")]
        public Order CaculatePrice(Order order)
        {
            if (order.ProductList == null || order.ProductList.Count == 0)
            {
                return order;
            }
            order.MemLoginID = GetCurrentUser();
            return orderService.CaculatePrice(order);
        }

        [GET("api/order/invoicetype/list")]
        public List<string> GetInvoiceTaxType()
        {
            return orderService.GetInvoiceTaxType();
        }

        [PUT("api/order/DispatchPrice")]
        public decimal GetDispatchPrice(Order order)
        {
            return orderService.GetDispatchPrice(order);
        }
        [PUT("api/order/InsurePrice")]
        public decimal GetInsurePrice(Order order)
        {
            return orderService.GetInsurePrice(order);
        }
        [GET("api/order/AdvancePayment")]
        public decimal GetAdvancePayment()
        {
            return orderService.GetAdvancePayment(GetCurrentUser());
        }
        [GET("api/order/PackFee")]
        public decimal GetPackFee(Guid packGuid, decimal totalPrice)
        {
            return orderService.GetPackFee(packGuid, totalPrice);
        }
        [GET("api/order/BlessCardFee")]
        public decimal GetBlessCardFee(Guid blessCardGuid, decimal totalPrice)
        {
            return orderService.GetBlessCardFee(blessCardGuid, totalPrice);
        }
        [PUT("api/order/FavourTicket")]
        public decimal GetFavourTicket(Order order)
        {
            return orderService.GetFavourTicket(order);
        }
        [PUT("api/order/Scores")]
        public decimal GetScores(Order order)
        {
            return orderService.GetScores(order);
        }
        [PUT("api/order/Tax")]
        public decimal GetTax(Order order, decimal allProductPrice)
        {
            return orderService.GetTax(order, allProductPrice);
        }
        [PUT("api/order/Payment")]
        public decimal GetPayment(Guid paymentGuid, decimal allProductPrice)
        {
            return orderService.GetPayment(paymentGuid, allProductPrice);
        }
    }
}